package com.xiaoyu.lingdian.controller;

import java.util.List;
import java.util.ArrayList;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.StringUtil;
import javax.servlet.http.HttpServletResponse;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import org.springframework.stereotype.Controller;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.controller.BaseController;
import com.xiaoyu.lingdian.entity.CoreRepairType;
import com.xiaoyu.lingdian.service.CoreRepairTypeService;
import com.xiaoyu.lingdian.vo.CoreRepairTypeVO;

@Controller
@RequestMapping(value="/coreRepairType")
public class CoreRepairTypeController extends BaseController {

	/**
	* CoreRepairType表
	*/
	@Autowired
	private CoreRepairTypeService coreRepairTypeService;
	
	/**
	* 添加
	*
	* @param crrteName 型号名称
	* @param crrteColor 颜色列表（用颜色|颜色隔开）
	* @param crrteFault 故障类型列表（用故障类型=价格|故障类型=价格隔开）
	* @return
	*/
	@RequestMapping(value="/add/coreRepairType", method=RequestMethod.POST)
	public void addCoreRepairType (String crrteName, String crrteColor, String crrteFault, HttpServletResponse response) {
		logger.info("[CoreRepairTypeController]:begin addCoreRepairType");
		
		if (StringUtil.isEmpty(crrteName)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[型号名称]不能为空!"), response);
			return;
		}

		if (StringUtil.isEmpty(crrteColor)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[颜色列表]不能为空!"), response);
			return;
		}
				CoreRepairType coreRepairType = new CoreRepairType();
		String uuid = RandomUtil.generateString(16);
		coreRepairType.setCrrteUuid(uuid);
		coreRepairType.setCrrteName(crrteName);
		coreRepairType.setCrrteColor(crrteColor);
		coreRepairType.setCrrteFault(crrteFault);

		coreRepairTypeService.insertCoreRepairType(coreRepairType);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "新增成功!"),response);
		logger.info("[CoreRepairTypeController]:end addCoreRepairType");

	}

	/**
	* 修改
	*
	* @param crrteUuid 标识UUID
	* @param crrteName 型号名称
	* @param crrteColor （用颜色|颜色隔开）
	* @param crrteFault 故障类型列表（用故障类型=价格|故障类型=价格隔开）
	* @return
	*/
	@RequestMapping(value="/update/coreRepairType", method=RequestMethod.POST)
	public void updateCoreRepairType (String crrteUuid, String crrteName, String crrteColor, String crrteFault, HttpServletResponse response) {
		logger.info("[CoreRepairTypeController]:begin updateCoreRepairType");
		if (StringUtil.isEmpty(crrteUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crrteName)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[型号名称]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crrteColor)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[颜色列表]不能为空!"), response);
			return;
		}
				CoreRepairType coreRepairType = new CoreRepairType();
		coreRepairType.setCrrteUuid(crrteUuid);
		coreRepairType.setCrrteName(crrteName);
		coreRepairType.setCrrteColor(crrteColor);
		coreRepairType.setCrrteFault(crrteFault);

		coreRepairTypeService.updateCoreRepairType(coreRepairType);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "修改成功!"),response);
		logger.info("[CoreRepairTypeController]:end updateCoreRepairType");

	}

	/**
	* 删除
	*
	* @param crrteUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteCoreRepairType (String crrteUuid, HttpServletResponse response) {
		logger.info("[CoreRepairTypeController]:begin deleteCoreRepairType");
		if (StringUtil.isEmpty(crrteUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreRepairType coreRepairType = new CoreRepairType();
		coreRepairType.setCrrteUuid(crrteUuid);

		coreRepairTypeService.deleteCoreRepairType(coreRepairType);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除成功!"),response);
		logger.info("[CoreRepairTypeController]:end deleteCoreRepairType");

	}

	/**
	* 批量删除
	*
	* @param crrteUuids UUID集合

	* @return
	*/
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatchCoreRepairType (String crrteUuids, HttpServletResponse response) {
		logger.info("[CoreRepairTypeController]:begin deleteBatchCoreRepairType");
		if (StringUtil.isEmpty(crrteUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[UUID集合]不能为空!"), response);
			return;
		}
		String[] uuids=crrteUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < uuids.length; i++) {
			list.add(uuids[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		coreRepairTypeService.deleteBatchByIds(list);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "批量删除成功!"),response);
		logger.info("[CoreRepairTypeController]:end deleteBatchCoreRepairType");

	}

	/**
	* 获取单个
	*
	* @param crrteUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsCoreRepairType (String crrteUuid, HttpServletResponse response) {
		logger.info("[CoreRepairTypeController]:begin viewsCoreRepairType");
		if (StringUtil.isEmpty(crrteUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreRepairType coreRepairType = new CoreRepairType();
		coreRepairType.setCrrteUuid(crrteUuid);

		coreRepairType = coreRepairTypeService.getCoreRepairType(coreRepairType);

		CoreRepairTypeVO coreRepairTypeVO = new CoreRepairTypeVO();
		coreRepairTypeVO.convertPOToVO(coreRepairType);
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "获取单个信息成功", coreRepairTypeVO), response);
		logger.info("[CoreRepairTypeController]:end viewsCoreRepairType");

	}

	/**
	* 获取列表<List>	
	* @return
	*/
	@RequestMapping(value="/find/all", method=RequestMethod.POST)
	public void findCoreRepairTypeList (HttpServletResponse response) {
		logger.info("[CoreRepairTypeController]:begin findCoreRepairTypeList");
		List<CoreRepairType> lists = coreRepairTypeService.findCoreRepairTypeList();
		List<CoreRepairTypeVO> vos = new ArrayList<CoreRepairTypeVO>();
		CoreRepairTypeVO vo;
		for (CoreRepairType coreRepairType : lists) {
			vo = new CoreRepairTypeVO();
			vo.setCrrteUuid(coreRepairType.getCrrteUuid());
			vo.setCrrteName(coreRepairType.getCrrteName());
			vo.setCrrteColor(coreRepairType.getCrrteColor());
			vo.setCrrteFault(coreRepairType.getCrrteFault());
			vos.add(vo);
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "list列表获取成功!", vos),response);
		logger.info("[CoreRepairTypeController]:end findCoreRepairTypeList");

	}

	/**
	* 获取列表<Page>	
	* @param pageNum 页码
	* @param pageSize 页数
	* @param crrteName 型号名称
	* @return
	*/
	@RequestMapping(value="/find/by/cnd", method=RequestMethod.POST)
	public void findCoreRepairTypePage (Integer pageNum, Integer pageSize, String crrteName, HttpServletResponse response) {
		logger.info("[CoreRepairTypeController]:begin findCoreRepairTypePage");
		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		Page<CoreRepairType> page = coreRepairTypeService.findCoreRepairTypePage(pageNum, pageSize, crrteName);
		Page<CoreRepairTypeVO> pageVO = new Page<CoreRepairTypeVO>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());
		List<CoreRepairTypeVO> vos = new ArrayList<CoreRepairTypeVO>();
		CoreRepairTypeVO vo;
		for (CoreRepairType coreRepairType : page.getResult()) {
			
			vo = new CoreRepairTypeVO();
			vo.setCrrteUuid(coreRepairType.getCrrteUuid());
			vo.setCrrteName(coreRepairType.getCrrteName());
			vo.setCrrteColor(coreRepairType.getCrrteColor());
			vo.setCrrteFault(coreRepairType.getCrrteFault());
			
			vos.add(vo);
		}
		pageVO.setResult(vos);
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "page列表获取成功!", pageVO),response);
		logger.info("[CoreRepairTypeController]:end findCoreRepairTypePage");

	}
//<=================定制内容开始==============
//==================定制内容结束==============>

}